package cn.neusoft.controller;


import cn.neusoft.domain.CargoDescription;
import cn.neusoft.domain.Warehouse;
import cn.neusoft.service.DispatchService;
import cn.neusoft.service.WarehouseAdministrateService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/dispatch")
public class DispatchController {
    @Autowired
    private DispatchService dispatchService;
    @Autowired
    private WarehouseAdministrateService administrateService;

    @ApiOperation("获取某一仓库的所有缺乏的商品描述")
    @GetMapping("/lackSearALl/{warehouseName}")
    public synchronized ResultBean lackCargoDesSearchAll(@PathVariable("warehouseName") String warehouseName) {
        ResultBean resultBean = null;

        List<String> list = dispatchService.lackCargoDesSearchAll(warehouseName);
        resultBean = ResultBean.success(list);

        return resultBean;
    }
    @ApiOperation("判断某个仓库中某类商品是否缺乏")
    @PostMapping("/lackSear")
    public synchronized ResultBean findCargoIsLack(@RequestBody Map<String, String> map) {
        ResultBean resultBean = null;

        if(map != null){
            try{
                String warehouseName = map.get("warehouseName");
                String cargoDesName = map.get("cargoDesName");
                int reply = dispatchService.findCargoIsLack(warehouseName, cargoDesName);
                switch(reply){
                    case 1:
                        resultBean = ResultBean.success("缺乏");
                        break;
                    case 0:
                        resultBean = ResultBean.success("不缺乏");
                        break;
                    default:
                        resultBean = ResultBean.error(4012, "未知错误");
                        break;
                }
            }catch(Exception e){
                e.printStackTrace();
                resultBean = ResultBean.error(4013, "错误");
            }

        }
        else{
            resultBean = ResultBean.error(4010, "输入为空");
        }

        return resultBean;
    }

    @PostMapping("/getCargoQuaInWarehouse")
    public synchronized ResultBean getCargoQuaInWarehouse(@RequestBody Map<String, String> map){
        ResultBean resultBean = null;

        if(map != null){
            try{
                String warehouseName = map.get("warehouseName");
                String cargoDesName = map.get("cargoDesName");

                double reply = dispatchService.getCarQuanInWarehouse(warehouseName, cargoDesName);
                if(reply == -1){
                    resultBean = ResultBean.error(4011, "不存在此商品");
                }
                else{
                    resultBean = ResultBean.success(reply);
                }
            }catch(Exception e){
                e.printStackTrace();
                resultBean = ResultBean.error(4013, "错误");
            }

        }
        else{
            resultBean = ResultBean.error(4010, "输入为空");
        }

        return resultBean;
    }

//    public double getCargoQuaInWarehouse(String warehouseName, String desName){
//      return dispatchService.getCarQuanInWarehouse(warehouseName, desName);
//    }


//    private class inspectCarInWarehouse{
//        private String warehouseName;
//        private String variety;
//        private String desID;
//        private String desName;
//        private String curQuantities;
//        private String safeQuantities;
//        private String lackQuantities;
//        private String inputQuantities;
//    }
}
